<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>03_计算器练习</title>
</head>

<body>
<input type="text" placeholder="请输入数字1" id="num1" ><br>
<input type="text" placeholder="请输入数字2" id="num2" ><br>
<button onclick="calc('+')">加</button>
<button onclick="calc('-')">减</button>
<button onclick="calc('*')">乘</button>
<button onclick="calc('/')">除</button>
<h4>运算结果：<span></span></h4>

<script>
    function calc(op) {
        //获取数字1和数字2
        let num1=parseFloat(document.getElementById('num1').value);
        let num2=parseFloat(document.getElementById('num2').value);
        let spanE = document.querySelector('span');
        switch (op) {
            case '+':
                spanE.innerHTML = num1 + num2;
                break;
            case '-':
                spanE.innerHTML = num1 - num2;
                break;
            case '*':
                spanE.innerHTML = num1 * num2;
                break;
            case '/':
                if (num2 != 0){
                    spanE.innerHTML = num1 / num2;
                    break;
                }else  {
                   alert('除数不能为0')  ;
                }

        }
    }
</script>
</body>
</html>